package com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReviewTestPaper.aibkQuesReviewZHT

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReviewTestPaper.QuesReviewInforIndexHelp.getChildListBean
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.TestPaperReviewInfor
import com.lancoo.znbkxx.teacher.widget.CustomTextViewToPicture
import kotlinx.android.synthetic.main.aibk_adapter_item_child_jdt.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_childask.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_itemanalysis.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_itemanswer.view.*

class QuesReviewAdapterChildJdt(
        val activity: AppCompatActivity,
        val testPaperReviewInfor: TestPaperReviewInfor,
        val index_A:Int,
        val index_B:Int,
        val index_C:Int
) : KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        var rootView: View? = null
        if (position == 0) {
            rootView = LayoutInflater.from(activity)
                    .inflate(R.layout.aibk_adapter_item_childask, parent, false)
        } else {
            rootView = LayoutInflater.from(activity)
                    .inflate(R.layout.aibk_adapter_item_child_jdt, parent, false)
        }
        return rootView
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {
        if (position == 0) {
            //隐藏播放器
            holder.convertView.IvChildAudioPlayA.visibility = View.GONE

            var childAsk = getChildListBean(testPaperReviewInfor, index_A, index_B, index_C).ChildAsk
            val genreID = testPaperReviewInfor.TypeList[index_A].QuesDetailList[index_B].GenreID
            val sortIndex = getChildListBean(testPaperReviewInfor, index_A, index_B, index_C).ItemList[0].SortIndex
            val childAnswerType = getChildListBean(testPaperReviewInfor, index_A, index_B, index_C).ChildAnswerType
            val typeNo = testPaperReviewInfor.TypeList[index_A].QuesDetailList[index_B].TypeNo
            if (typeNo == "Q") {//阅读填空
                val childListSize = testPaperReviewInfor.TypeList[index_A].QuesDetailList[index_B].ChildList.size
                if(childListSize == 1){
                    holder.convertView.TvChildAskPositionX.visibility = View.GONE
                    holder.convertView.TvChildAsk.visibility = View.GONE
                }else{
                    holder.convertView.TvChildAskPositionX.visibility = View.VISIBLE
                    holder.convertView.TvChildAsk.visibility = View.VISIBLE
                }

            } else if (typeNo == "X" || typeNo == "G") {//X:阅读简单 G:听力简答
                holder.convertView.TvChildAsk.visibility = View.VISIBLE
            } else if (typeNo == "k") {//阅读综合
                if (childAnswerType == 32) {//匹配题综合
                    holder.convertView.TvChildAsk.visibility = View.GONE
                } else {
                    holder.convertView.TvChildAsk.visibility = View.VISIBLE
                }
            } else if (typeNo == "b" || typeNo == "V" || typeNo == "d") {//b:补全对话 V:连词成句 d:看图写句子
                val quesChildNum = testPaperReviewInfor.TypeList[index_A].QuesDetailList[index_B].QuesChildNum
                if(quesChildNum == 1){
                    holder.convertView.TvChildAskPositionX.visibility = View.VISIBLE
                }else{
                    holder.convertView.TvChildAskPositionX.visibility = View.GONE
                }
                holder.convertView.TvChildAsk.visibility = View.GONE
            } else if (typeNo == "g") {//翻译题
                if (genreID == 39 || genreID == 41) {
                    //39:GenreName=篇章英译中//41:GenreName=篇章中译英
                    if (genreID == 39) {
                        childAsk = "请将上面的英文篇章翻译成中文"
                    } else if (genreID == 41) {
                        childAsk = "请将上面的中文篇章翻译成英文"
                    }
                    holder.convertView.TvChildAsk.visibility = View.VISIBLE
                }else {
                    val quesBody = testPaperReviewInfor.TypeList[index_A].QuesDetailList[index_B].QuesBody
                    if(quesBody.isEmpty()){
                        holder.convertView.TvChildAsk.visibility = View.GONE
                    }else{
                        holder.convertView.TvChildAsk.visibility = View.VISIBLE
                    }
                }
            }
            holder.convertView.TvChildAskPositionX.text = "$sortIndex. "
            CustomTextViewToPicture(activity, holder.convertView.TvChildAsk, childAsk)
            holder.convertView.IvChildAudioPlayA.visibility = View.GONE
        } else {
            val genreID = testPaperReviewInfor.TypeList[index_A].QuesDetailList[index_B].GenreID
            val sortIndexType = getChildListBean(testPaperReviewInfor, index_A, index_B, index_C).SortIndexType
            var sortIndex = getChildListBean(testPaperReviewInfor, index_A, index_B, index_C).ItemList[position - 1].SortIndex
            var itemAnswer = getChildListBean(testPaperReviewInfor, index_A, index_B, index_C).ItemList[position - 1].ItemAnswer
            var itemAnalysis = getChildListBean(testPaperReviewInfor, index_A, index_B, index_C).ItemList[position - 1].ItemAnalysis
            if(genreID == 39||genreID == 41){
                holder.convertView.TvChildAskPositionY.visibility = View.GONE
            }else {
                if (sortIndexType == 0) {
                    holder.convertView.TvChildAskPositionY.visibility = View.GONE
                } else {
                    holder.convertView.TvChildAskPositionY.visibility = View.VISIBLE
                    holder.convertView.TvChildAskPositionY.text = "$sortIndex. "
                }
            }
            CustomTextViewToPicture(activity,holder.convertView.TvItemAnswer, LancooMethod.getNewItemAnswer(itemAnswer, false))
            CustomTextViewToPicture(activity,holder.convertView.TvItemAnalysis, LancooMethod.getNewItemAnalysis(itemAnalysis))
        }
    }

    private fun getItemTotalSize(): Int {
        return getChildListBean(testPaperReviewInfor, index_A, index_B, index_C).ItemList.size
    }

    override fun getItemCount(): Int {
        return getItemTotalSize() + 1
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

}